﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Life.DAL;
using Life.Model;
using System.Linq.Expressions;
using System.Data;

namespace Life.BLL
{
    public class BLLifingCost
    {
        DLLifingCost dLLifingCost = new DLLifingCost();

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns>影响的行数</returns>
        public int Add(LifingCost lifingCost)
        {
            return dLLifingCost.Add(lifingCost);
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="list"></param>
        /// <returns>影响的行数</returns>
        public int Add(List<LifingCost> list)
        {
            return dLLifingCost.Add(list);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns>影响的行数</returns>
        public int Update(LifingCost lifingCost)
        {
            return dLLifingCost.Update(lifingCost);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>影响的行数</returns>
        public int Delete(string ids)
        {
            return dLLifingCost.Delete(ids);
        }

        /// <summary>
        /// 删除所有数据
        /// </summary>
        /// <returns></returns>
        public int Delete()
        {
            return dLLifingCost.Delete();
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns>数据集合</returns>
        public List<LifingCost> Select()
        {
            return dLLifingCost.Select();
        }

        /// <summary>
        /// 按条件查询所有符合条件的数据(不分页)
        /// </summary>
        /// <param name="filter">筛选条件</param>
        /// <returns>信息集合</returns>
        public List<LifingCost> Select(Expression<Func<LifingCost, bool>> filter)
        {
            return dLLifingCost.Select(filter);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageSize">每页多少条数据</param>
        /// <param name="start">排除多少条数据</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="total">总共多少条</param>
        /// <returns>数据集合</returns>
        public List<VLifingCost> Select(int pageSize, int start, String filter, out int total, params Object[] values)
        {
            return dLLifingCost.Select(pageSize, start, filter, out total, values);
        }

        /// <summary>
        /// 根据编号查询
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>数据集合</returns>
        public LifingCost Select(string id)
        {
            return dLLifingCost.Select(id);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageSize">每页多少条数据</param>
        /// <param name="start">排除多少条数据</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="total">总共多少条</param>
        /// <returns>数据集合</returns>
        public List<VLifingCost> Select(int pageSize, int start, Expression<Func<VLifingCost, bool>> filter, out int total)
        {
            return dLLifingCost.Select(pageSize, start, filter, out total);

        }

        /// <summary>
        /// 根据月份汇总生活费消费信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetCollectionByMonth()
        {
            return dLLifingCost.GetCollectionByMonth();
        }

        /// <summary>
        /// 根据月份汇总生活费
        /// </summary>
        /// <param name="time">时间，如:2013-1</param>
        /// <returns></returns>
        public DataTable GetCollectionByDay(String time)
        {
            return dLLifingCost.GetCollectionByDay(time);
        }

        /// <summary>
        /// 根据月份汇总生活费类型
        /// </summary>
        /// <param name="time">时间，如:2013-1</param>
        /// <returns></returns>
        public DataTable GetCollectionByType(String time)
        {
            return dLLifingCost.GetCollectionByType(time);
        }


        /// <summary>
        /// 将dataTable保存到数据库
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public String SaveData(DataTable dt,String userId)
        {
            List<LifingCost> list = new List<LifingCost>();
            String msg = "";
            foreach (DataRow row in dt.Rows)
            {
                LifingCost index = new LifingCost();
                String type = row["消费类型"].ToString();
                //判断类型是否存在
                List<Diction> dictions = new BLDiction().Select(f => f.Name == type && f.ParentId == 1000300000);
                if (dictions.Count <= 0)
                {
                    msg += "<br/>" + type + "不存在";
                    continue;
                }

                DateTime time;
                //判断时间是否正确
                if (!DateTime.TryParse(row["消费时间"].ToString(), out time))
                {
                    msg += "<br/>" + row["消费时间"] + "不是时间类型";
                    continue;
                }
                else
                    index.Time = time;

                double price;
                     
                //判断金额是否正确
                if (!double.TryParse(row["消费金额"].ToString(), out price))
                {
                    msg += "<br/>" + row["消费金额"] + "不是数字类型";
                    continue;
                }
                else
                    index.Price = price;

                index.Id = Guid.NewGuid().ToString();
                index.Reason = row["消费名称"].ToString();
                index.CostTypeId = dictions[0].Id;
                index.Notes = row["备注"].ToString();
                index.CreateBy = index.UpdateBy = userId;
                index.CreateTime = index.UpdateTime = DateTime.Now;
                list.Add(index);
            }

            int result=dLLifingCost.Add(list);
            if (result > 0)
                return "成功保存" + result + "条数据." + msg;
            else
                return msg;
        }
    }
}


